package com.buybuyyi.stylistcrm.user.services;

import com.buybuyyi.stylistcrm.user.dao.AuditingDao;
import com.buybuyyi.stylistcrm.user.dao.impl.ChargeOfStoreAuditingDaoImpl;
import com.buybuyyi.stylistcrm.user.dao.impl.DesignerAuditingDaoImpl;
import com.buybuyyi.stylistcrm.user.dao.impl.StylistAuditingDaoImpl;
import com.buybuyyi.stylistcrm.user.mapper.RequisitionAuditRecordMapper;
import com.buybuyyi.stylistcrm.user.mapper.UserRequisitionMapper;
import com.buybuyyi.stylistcrm.user.po.RequisitionAuditRecord;
import com.buybuyyi.stylistcrm.user.po.UserRequisitionWithBLOBs;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import stylistcrm.core.services.order.exception.BizException;
import stylistcrm.core.services.user.EnumUserType;
import stylistcrm.core.services.user.User;
import stylistcrm.core.services.user.requisition.Requisition;
import stylistcrm.core.services.user.requisition.EnumRequisitionState;
import stylistcrm.core.services.user.requisition.RequisitionDto;
import stylistcrm.core.services.user.service.AuditingService;
import stylistcrm.core.services.user.service.UserService;

import java.util.Date;
import java.util.List;

/**
 * Created by yanwc on 2016/10/9.
 * 会员申请审核服务
 */
@Service
public class AuditingServiceImpl implements AuditingService {

    private static Logger logger = LogManager.getLogger(AuditingServiceImpl.class);

    @Autowired
    public RequisitionAuditRecordMapper requisitionAuditRecordMapper;

    @Autowired
    public UserService userService;

    @Autowired
    public UserRequisitionMapper userRequisitionMapper;

    @Autowired
    StylistAuditingDaoImpl stylistAuditingDao;

    @Autowired
    DesignerAuditingDaoImpl designerAuditingDao;

    @Autowired
    ChargeOfStoreAuditingDaoImpl chargeOfStoreAuditingDao;

    public boolean audit(RequisitionDto requistionDto, boolean isPass, String message) throws BizException {
        if(requistionDto == null){
            return false;
        }
        AuditingDao audit=null;
        if(EnumUserType.STYLIST.getCode().equals(requistionDto.getUserType())){
            audit=stylistAuditingDao;
        }else if(EnumUserType.DESIGNER.getCode().equals(requistionDto.getUserType())){
            audit=designerAuditingDao;
        }else{
            audit=chargeOfStoreAuditingDao;
        }
        if(audit != null && audit.isAudit(requistionDto)){
            User user = userService.getCurrentUser();
            if(user ==null){
                logger.warn("获取当前用户信息失败，审核用户失败");
                return false;
            }
            RequisitionAuditRecord record = new RequisitionAuditRecord();
            record.setCreateTime(new Date());
            record.setAuditorId(user.getId());
            record.setAuditorName(user.getUserName());
            record.setMessage(message);
            record.setRequisitionId(requistionDto.getId());
            //更新审核状态
            UserRequisitionWithBLOBs userRequisition = new UserRequisitionWithBLOBs();
            userRequisition.setId(requistionDto.getId());
            if(isPass){
                audit.pass(requistionDto,user);
                userRequisition.setState(EnumRequisitionState.Pass.getValue());
                record.setState(EnumRequisitionState.Pass.getName());
            }else{
                userRequisition.setState(EnumRequisitionState.Reject.getValue());
                record.setState(EnumRequisitionState.Reject.getName());
            }
            requisitionAuditRecordMapper.insert(record);
            userRequisitionMapper.updateByPrimaryKeySelective(userRequisition);
        }
        return false;
    }
}
